package com.pjmike.netty.server.handler;

import com.pjmike.netty.client.NettyMessageUtil;
import com.pjmike.netty.protocol.message.command.Command;
import com.pjmike.netty.protocol.message.packet.request.AuthRequestPacket;
import com.pjmike.netty.protocol.message.packet.response.AuthResponsePacket;
import com.pjmike.netty.utils.SpringContextUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 *
 */
@Slf4j
@ChannelHandler.Sharable
public class AuthHandler extends SimpleChannelInboundHandler<AuthRequestPacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, AuthRequestPacket msg) throws Exception {
        log.info("获取到客户端发来的认证报文，报文体：{}", msg);
        if(Objects.nonNull(msg)){
            AuthResponsePacket authResponsePacket = new AuthResponsePacket();
            BeanUtils.copyProperties(msg, authResponsePacket);
            //TODO  认证操作
            authResponsePacket.setApply((short)1);
            NettyMessageUtil.registerSB(msg.getSbbh(),ctx);
            authResponsePacket.setSj(System.currentTimeMillis());
            ctx.writeAndFlush(authResponsePacket);
        }
    }
}